package com.ef.web.action;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.ELeadsDealType;
import com.ef.constant.ELeadsStatus;
import com.ef.constant.ELeadsType;
import com.ef.domain.Leads;
import com.ef.domain.LeadsDealHistory;
import com.ef.domain.School;
import com.ef.domain.User;
import com.ef.util.CustomExample;
import com.ef.util.LeadsExample;

public class CCAssignAction extends LeadsAction {
	private static final long serialVersionUID = 1L;

	private Map<String, String> ccs;

	private boolean noCc = true;

	private boolean me = false;

	@SuppressWarnings("serial")
	public String list() {
		fetchListData();
		String tel = this.getModel().getTelephone();
		if (tel != null && tel.trim().length() > 7) {
			List<Leads> leads = leadsService.findByTelephoneAccurate(tel);
			if (leads.size() == 1) {
				Leads lead = leads.get(0);
				String info = "";
				if (lead.getSchool() == null) {// 没学校
					info = lead.getName() + " no school ,TM ";
					if (lead.getTm() != null) {
						info = info + " ,TM " + lead.getTm().getName();
					} else {
						info = info + " no TM";
					}
					this.resultError(info, "list");
					// System.out.println(info);
					return LIST;
				} else if (!lead.getSchool().getId().equals(
						getSessionUser().getSchool().getId())) {
					info = lead.getName() + " at school "
							+ lead.getSchool().getName();
					if (lead.getTm() != null) {
						info = info + " ,TM " + lead.getTm().getName();
					} else {
						info = info + " no TM";
					}
					this.resultError(info, "list");
					// System.out.println(info);
					return LIST;
				}
			}
		}

		CustomExample<Leads> example = new LeadsExample(this.getModel(),
				Leads.class) {
			public void appendCondition(Criteria criteria) {
				super.appendCondition(criteria);
				School s = getSessionUser().getSchool();
				if (s != null) {
					criteria.add(Restrictions.eq("school", s));
				}
				if (me) {//分配给CM本人的leads
					if (getSessionUser() != null){
						criteria.add(Restrictions.eq("cc", getSessionUser()));
					}
				} else {
					if (noCc) {
						if (entity.getCc() == null
								|| entity.getCc().getName() == null
								|| entity.getCc().getName().length() == 0) {
							//无设置CC 的查询选项
							criteria.add(Restrictions.isNull("cc"));
							criteria
							.add(Restrictions
									.or(
											Restrictions
													.eq(
															"leadsStatus",
															ELeadsStatus.ShowUp),
											Restrictions
													.or(
															Restrictions
																	.and(
																			Restrictions
																					.eq(
																							"leadsStatus",
																							ELeadsStatus.New),
																			Restrictions
																					.eq(
																							"leadsType",
																							ELeadsType.CarOwner)),
															Restrictions
																	.and(
																			Restrictions
																					.eq(
																							"leadsStatus",
																							ELeadsStatus.New),
																			Restrictions
																					.eq(
																							"leadsType",
																							ELeadsType.Internet)))));
						}
					} else{//包含CC
						criteria.add(Restrictions.isNotNull("cc"));
					}
				}
			}
		};
		example.addBetweenDateProperty("createTime", selCreateStartTime,
				selCreateEndTime);
		example.addBetweenDateProperty("setApptTime", selSetApptStartTime,
				selSetApptEndTime);
		example.addBetweenDateProperty("showTime", selShowStartTime,
				selShowEndTime);
		example.addBetweenDateProperty("lastEventTime", selLastTouchStartTime,
				selLastTouchEndTime);
		example.addBetweenDateProperty("payTime", selPayStartTime,
				selPayEndTime);
		example.addBetweenDateProperty("ccAssignTime", selAssignTmStartTime,
				selAssignTmEndTime);
		this.listResult = leadsService.findByExample(example, this
				.getStartIndex(), this.getPageSize());
		return LIST;
	};

	public String assignToCc() {
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		String cc = getRequest().getParameter("cc_id");
		User user = userService.findById(Long.valueOf(cc));
		for (String i : id) {
			Leads l = leadsService.findById(Long.valueOf(i));
			//
			LeadsDealHistory dh = new LeadsDealHistory();
			dh.setCurState(user.getName());
			if (l.getCc() != null) {
				dh.setPreState(l.getCc().getName());
			}
			dh.setDealTime(new Date());
			dh.setOperUser(this.getSessionUser());
			dh.setType(ELeadsDealType.AssignedToCC);
			dh.setLeads(l);
			leadsDealHistoryService.saveOrUpdate(dh);
			//
			l.setCc(user);
			l.setCcAssignTime(new Date());
			leadsService.saveOrUpdate(l);
		}
		fetchListData();
		return OK;
	}

	protected void fetchListData() {
		super.fetchListData();
		Map<String, String> map = new LinkedHashMap<String, String>();
		School school = getSessionUser().getSchool();
		List<User> users = null;
		if (school == null)
			users = userService.findAllCc();
		else {
			users = userService.findAllCcSameSchool(school);
		}

		for (User u : users) {
			map.put(u.getId().toString(), u.getName());
		}
		ccs = map;
	}

	public Map<String, String> getCcs() {
		return ccs;
	}

	public void setCcs(Map<String, String> ccs) {
		this.ccs = ccs;
	}

	public boolean isNoCc() {
		return noCc;
	}

	public void setNoCc(boolean noCc) {
		this.noCc = noCc;
	}

	public boolean isMe() {
		return me;
	}

	public void setMe(boolean me) {
		this.me = me;
	}

}
